/*
 * Aboboo Android
 * Copyright (C) 2012 www.aboboo.com
 * 
 */
package com.aboboo.android.activity;

import com.aboboo.android.R;

import android.app.ActivityGroup;
import android.app.AlertDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;
import android.widget.BaseAdapter;
import android.widget.AdapterView;

import android.os.Bundle;
import android.util.Log;

import com.aboboo.android.constant.AppConstant;

/**
 * Aboboo Android 主界面，使用tab标签菜单实现相应功能的切换和选择
 * 
 * @author fangpeng
 * @version 1.0 2012-07-24
 */
public class AbobooActivity extends ActivityGroup {

	private final static String Activity_Tag = "AbobooActivity";

	private ViewFlipper viewFlipper;
	private GridView menuGridView;

	// 底部菜单名字
	private String[] menuGridViewNames = null;
	// 底部菜单图标集
	private int[] menuGridViewImgs = null;

	// 没有选中时候的图标样式
	// private int[] menuGridVIewUnselectedImgs = null;

	// 上次选中的位置
	private int lastClickPosition;
	// 当前选中的位置
	private int clickPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 设置隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.aboboo);

		initRes();
		findViews();
		setValues();
		setListeners();
	}

	// 初始化菜单名称和图片数组资源
	private void initRes() {
		// 初始化功能菜单的文字
		menuGridViewNames = new String[AppConstant.MAIN_MENU_COUNT];
		menuGridViewNames[0] = getString(R.string.studying_now);
		menuGridViewNames[1] = getString(R.string.my_courses);
		menuGridViewNames[2] = getString(R.string.online_course_groups);
		menuGridViewNames[3] = getString(R.string.sysfunc_set);
		menuGridViewNames[4] = getString(R.string.more_info);

		// 初始化功能菜单图标
		menuGridViewImgs = new int[AppConstant.MAIN_MENU_COUNT];
		menuGridViewImgs[0] = R.drawable.tab_study_now;
		menuGridViewImgs[1] = R.drawable.tab_my_courses;
		menuGridViewImgs[2] = R.drawable.tab_online_courses;
		menuGridViewImgs[3] = R.drawable.tab_sysfunc;
		menuGridViewImgs[4] = R.drawable.tab_more_info;

	}

	private void findViews() {
		viewFlipper = (ViewFlipper) findViewById(R.id.menu_view_content);
		menuGridView = (GridView) findViewById(R.id.menu_gridview);

	}

	// 设置表格布局的值
	private void setValues() {
		menuGridView.setAdapter(new BottomMenuGridView(AbobooActivity.this));
		menuGridView.setNumColumns(menuGridViewImgs.length);
		menuGridView.setGravity(Gravity.CENTER);
		menuGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		menuGridView.setBackgroundResource(R.drawable.tab_bg);
		// 进入正在学习页面
		switchActivity(AppConstant.STUDYING_NOW_ACTIVITY_INDEX);
	}

	// 切换activity
	private void switchActivity(int index) {
		Intent intent = null;
		String activityId = null;

		switch (index) {
		case AppConstant.STUDYING_NOW_ACTIVITY_INDEX:
			// 正在学习
			intent = new Intent(AbobooActivity.this, StudyingNowActivity.class);
			activityId = AppConstant.STUDYING_NOW_ACTIVITY;
			break;
		case AppConstant.MY_COURSES_ACTIVITY_INDEX:
			// 我的课件
			intent = new Intent(AbobooActivity.this, MyCoursesActivity.class);
			activityId = AppConstant.MY_COURSES_ACTIVITY;
			break;
		case AppConstant.COURSE_GROUPS_LIST_ACTIVITY_INDEX:
			// 在线课件组资源，课件组合课件信息页面
			intent = new Intent(AbobooActivity.this,
					CourseGroupsListActivity.class);
			activityId = AppConstant.COURSE_GROUPS_LIST_ACTIVITY;
			break;
		case AppConstant.SYSFUNC_SET_ACTIVITY_INDEX:
			// 功能设置
			intent = new Intent(AbobooActivity.this, SysfuncSetActivity.class);
			activityId = AppConstant.SYSFUNC_SET_ACTIVITY;
			break;
		case AppConstant.MORE_INFO_ACTIVITY_INDEX:
			intent = new Intent(AbobooActivity.this, MoreinfoActivity.class);
			activityId = AppConstant.MORE_INFO_ACTIVITY;
			break;

		default:
			break;

		}

		if (activityId != null) {
			toActivity(activityId, intent);
		}

	}

	// 切换到指定的Activity
	public void toActivity(String activityId, Intent intent) {
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		View view = this.getLocalActivityManager()
				.startActivity(activityId, intent).getDecorView();
		// view.setAnimation(animation);
		viewFlipper.removeAllViews();
		viewFlipper.addView(view);
		viewFlipper.showNext();
	}

	// 添加相关的监听器
	private void setListeners() {
		menuGridView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long id) {
						// 日志调试信息
						lastClickPosition = clickPosition;
						clickPosition = position;
						// 重复选中不处理
						if (lastClickPosition == clickPosition) {
							return;
						}
						RelativeLayout layout = (RelativeLayout) menuGridView
								.getChildAt(position);
						layout.setBackgroundResource(R.drawable.title_bg);
						layout.getChildAt(0).setBackgroundResource(
								menuGridViewImgs[position]);

						for (int i = 0; i < menuGridViewNames.length; i++) {
							if (i != position) {
								RelativeLayout r_layout = (RelativeLayout) menuGridView
										.getChildAt(i);
								r_layout.setBackgroundDrawable(null);
								// 设置没有选中时候的背景图片
								// r_layout.getChildAt(0).setBackgroundResource(int
								// i);
							}
						}
						switchActivity(position);
					}

				});
	}

	// 底部菜单扩展适配器
	private class BottomMenuGridView extends BaseAdapter {

		private LayoutInflater inflater;

		public BottomMenuGridView(Context context) {
			// super();
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return menuGridViewNames.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MenuViewHolder viewHolder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.tab_item, null);
				viewHolder = new MenuViewHolder();
				viewHolder.imageView = (ImageView) convertView
						.findViewById(R.id.menu_image_item);
				viewHolder.textView = (TextView) convertView
						.findViewById(R.id.menu_text_item);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (MenuViewHolder) convertView.getTag();
			}
			viewHolder.imageView
					.setBackgroundResource(menuGridViewImgs[position]);
			viewHolder.textView.setText(menuGridViewNames[position]);
			return convertView;
		}

	}

	// 封装菜单的图片和文字
	private final class MenuViewHolder {
		public ImageView imageView;
		public TextView textView;

		// public MenuViewHolder(ImageView imageView, TextView textView) {
		// this.imageView = imageView;
		// this.textView = textView;
		// }
	}

	/**
	 * 设置menuGridView背景颜色
	 * 
	 * @param position
	 *            选中的位置
	 */
	public void setSelectedMenuGridViewBackground(int position) {
		for (int i = 0; i < menuGridViewNames.length; i++) {
			RelativeLayout r_layout = (RelativeLayout) menuGridView
					.getChildAt(i);
			r_layout.setBackgroundDrawable(null);
		}
		menuGridView.getChildAt(position).setBackgroundResource(
				R.drawable.title_bg);
	}

	public int getLastClickPosition() {
		return lastClickPosition;
	}

	public void setLastClickPosition(int lastClickPosition) {
		this.lastClickPosition = lastClickPosition;
	}

	public int getClickPosition() {
		return clickPosition;
	}

	public void setClickPosition(int clickPosition) {
		this.clickPosition = clickPosition;
	}

	public ViewFlipper getViewFlipper() {
		return viewFlipper;
	}

	public void setViewFlipper(ViewFlipper viewFlipper) {
		this.viewFlipper = viewFlipper;
	}

	public GridView getMenuGridView() {
		return menuGridView;
	}

	public void setMenuGridView(GridView menuGridView) {
		this.menuGridView = menuGridView;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 处理返回事件
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 这里放捕获按键处理事件
			// 可以弹出对话框询问是否退出程序
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.aboboo_tip);
			builder.setMessage(R.string.sure_exit);
			builder.setPositiveButton(R.string.confirm,
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setNegativeButton(R.string.cancel,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					});
			builder.create().show();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
